/**
 * @作者 Luke
 * @微信公号 欧诺的小书屋
 * @开源项目 $ http://7yue.pro
 * @免费专栏 $ http://course.7yue.pro
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2020-6-23 20:42
 */
package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.ParameterException;
import io.github.talelin.latticy.dto.course.CreateOrUpdateHomeworkDTO;
import io.github.talelin.latticy.mapper.HomeworkItemMapper;
import io.github.talelin.latticy.mapper.HomeworkMapper;
import io.github.talelin.latticy.model.HomeworkDO;
import io.github.talelin.latticy.model.HomeworkItemDO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HomeworkService extends ServiceImpl<HomeworkMapper, HomeworkDO> {

    @Autowired
    private HomeworkItemMapper homeworkItemMapper;

    /**
     * 根据teacherId courseId 获取作业列表
     * @param teacherId 教师id
     * @param courseId 课程id
     * @return HomeworkDO
     */
    public List<HomeworkDO> getHomework(Long teacherId, Long courseId){
        List<HomeworkDO> homeworkDOS = this.baseMapper.selectByTeacherIdAndCourseId(teacherId, courseId);
        if (homeworkDOS == null) {
            return new ArrayList<>();
        }
        return homeworkDOS;
    }

    /**Homework
     * 根据homework查询homework完成情况
     * @param hid 作业id
     * @return HomeworkItemDO
     */
    public List<HomeworkItemDO> getHomeworkDetail(Long hid){
        List<HomeworkItemDO> detail = this.homeworkItemMapper.getDetail(hid);
        if (detail == null) {
            return new ArrayList<>();
        }
        return detail;
    }

    /**
     * 老师布置作业
     * @param validator CreateOrUpdateHomeworkDTO
     */
    public void distributeHomework(CreateOrUpdateHomeworkDTO validator){
        HomeworkDO h = new HomeworkDO();
        h.setName(validator.getName());
        h.setDescription(validator.getDescription());
        h.setSetTime(validator.getSetTime());
        h.setEndTime(validator.getEndTime());
        h.setCourseId(validator.getCourseId());
        h.setTeacherId(validator.getTeacherId());
        int result = this.baseMapper.insert(h);
        if (result <= 0) {
            throw new ParameterException("布置作业出错了");
        }
    }


    /**
     * 教师修改作业
     * @param id homework_item_id
     * @param status 作业状态
     * @return booleanH
     */
    public boolean correction(Long id, String status, Long score){
        int i = this.homeworkItemMapper.correctionHomework(id, status, score);
        return i > 0;
    }
}
